/**
 * @file    drv_timer.c
 * @author  yhy
 * @brief   芯必达IM94x系列芯片, timer抽象层
 * @version 1.01
 * @date    2025-07-09
 *
 * @copyright Copyright (c) 2025
 *
 * @details
 * Change Logs:
 * Date           Author       Notes
 * 2025-07-09     yhy          the first version V1.00
 * 2025-07-11     yhy          所有函数和变量名修改为snf_开头 V1.01
 *
 */
#include "drv_timer.h"

#include "timer_drv.h"

/**
 * @brief TIMER硬件实例管理
 *
 */
typedef struct
{
    bool             initialized; /*!< Timer是否初始化 */
    timer_callback_f callback;    /*!< Timer中断回调函数 */
    IRQn_Type        tim_irq;     /*!< Timer中断号 */
} snf_timer_hw_manager_t;

static result_e snf_drv_timer_validate_instance(snf_timer_instance_e instance);
static uint32_t snf_drv_timer_get_hw_instance(snf_timer_instance_e instance);
static void     snf_drv_timer_config(const snf_timer_config_t *timer_config);

/* TIMER配置参数 */
timer_config_t timer_base_config = {
    .srcClk       = TIMER_CLK_SRC_PERI0,      /*!< Select a clock source */
    .intEnable    = true,                     /*!< Interrupt on or off */
    .counterUnits = TIMER_COUNTER_UNITS_TICKS /*!< Timer counter unit */
};

/* TIMER硬件实例管理列表 */
static snf_timer_hw_manager_t timer_hw_manager_list[TIMER_HW_INSTANCE_MAXSIZE] = {
    [TIMER_INSTANCE_TIMER0] = { .initialized = false, .callback = NULL, .tim_irq = TIMER0_IRQn },
    [TIMER_INSTANCE_TIMER1] = { .initialized = false, .callback = NULL, .tim_irq = TIMER1_IRQn },
    [TIMER_INSTANCE_TIMER2] = { .initialized = false, .callback = NULL, .tim_irq = TIMER2_IRQn },
    [TIMER_INSTANCE_TIMER3] = { .initialized = false, .callback = NULL, .tim_irq = TIMER3_IRQn },
};

/**
 * @brief 初始化TIMER, 初始化后TIMER开启
 *
 * @param timer_config  TIMER初始化参数
 * @return result_e     初始化结果, RESULT_STATUS_OK表示成功, 其他值表示失败
 */
result_e snf_drv_timer_init(const snf_timer_config_t *timer_config)
{
    if (timer_config == NULL) {
        return RESULT_STATUS_INVALID_PARAM;
    }

    /* 校验timer实例 */
    result_e result = snf_drv_timer_validate_instance(timer_config->instance);
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    /* 获取硬件实例编号 */
    uint32_t hw_instance = snf_drv_timer_get_hw_instance(timer_config->instance);
    if (hw_instance >= TIMER_HW_INSTANCE_MAXSIZE) {
        return RESULT_STATUS_INVALID_PARAM;
    }

    /* 硬件实例配置 */
    timer_hw_manager_list[hw_instance].callback = timer_config->callback;

    /* 初始化TIMER */
    snf_drv_timer_config(timer_config);
    TIMER_Init(hw_instance, &timer_base_config, true);

    /* 清除中断标志并使能中断 */
    NVIC_ClearPendingIRQ(timer_hw_manager_list[hw_instance].tim_irq);
    NVIC_EnableIRQ(timer_hw_manager_list[hw_instance].tim_irq);

    timer_hw_manager_list[hw_instance].initialized = true;

    return RESULT_STATUS_OK;
}

/**
 * @brief 重新启动定时器  注意：初始化完定时器后定时器是默认开启的
 *
 * @param instance        timer实例
 * @return result_e       启动结果, RESULT_STATUS_OK为成功, 其他值为失败
 */
result_e snf_drv_timer_start(snf_timer_instance_e instance)
{
    if (instance >= TIMER_INSTANCE_MAX || !timer_hw_manager_list[instance].initialized) {
        return RESULT_STATUS_INVALID_PARAM;
    }

    TIMER_StartCounter(snf_drv_timer_get_hw_instance(instance));
    return RESULT_STATUS_OK;
}

/**
 * @brief 关闭定时器
 *
 * @param instance        timer实例
 * @return result_e       关闭结果, RESULT_STATUS_OK为成功, 其他值为失败
 */
result_e snf_drv_timer_stop(snf_timer_instance_e instance)
{
    if (instance >= TIMER_INSTANCE_MAX || !timer_hw_manager_list[instance].initialized) {
        return RESULT_STATUS_INVALID_PARAM;
    }

    TIMER_StopCounter(snf_drv_timer_get_hw_instance(instance));
    return RESULT_STATUS_OK;
}

/**
 * @brief  配置TIMER
 *
 * @param timer_config timer配置参数
 */
static void snf_drv_timer_config(const snf_timer_config_t *timer_config)
{
    timer_base_config.prescaler    = timer_config->prescaler;
    timer_base_config.compareValue = timer_config->compare_value;
}

/**
 * @brief  获取硬件实例编号
 *
 * @param instance      timer实例
 * @return uint32_t     硬件实例编号
 */
static uint32_t snf_drv_timer_get_hw_instance(snf_timer_instance_e instance)
{
    static uint32_t instance_table[TIMER_INSTANCE_MAX] = { [TIMER_INSTANCE_TIMER0] = 0U,
                                                           [TIMER_INSTANCE_TIMER1] = 1U,
                                                           [TIMER_INSTANCE_TIMER2] = 2U,
                                                           [TIMER_INSTANCE_TIMER3] = 3U };

    return instance_table[(uint32_t)instance];
}

/**
 * @brief 校验timer实例
 *
 * @param instance    timer实例
 * @return result_e   校验结果
 */
static result_e snf_drv_timer_validate_instance(snf_timer_instance_e instance)
{
    if (instance >= TIMER_INSTANCE_MAX) {
        return RESULT_STATUS_INVALID_PARAM;
    }

    return RESULT_STATUS_OK;
}

void TIMER0_IRQHandler(void)
{
    TIMER_ClearCompareFlag(0U);

    timer_hw_manager_list[0U].callback();
}

void TIMER1_IRQHandler(void)
{
    TIMER_ClearCompareFlag(1U);

    timer_hw_manager_list[1U].callback();
}

void TIMER2_IRQHandler(void)
{
    TIMER_ClearCompareFlag(2U);

    timer_hw_manager_list[2U].callback();
}

void TIMER3_IRQHandler(void)
{
    TIMER_ClearCompareFlag(3U);

    timer_hw_manager_list[3U].callback();
}
